package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.GwContract;
import com.ruoyi.system.domain.GwCustomerInfo;
import com.ruoyi.system.mapper.GwCustomerInfoMapper;
import com.ruoyi.system.service.IGwContractService;
import com.ruoyi.system.service.IGwCustomerInfoService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 客户管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GwCustomerInfoServiceImpl implements IGwCustomerInfoService {
    @Resource
    private GwCustomerInfoMapper gwCustomerInfoMapper;
    @Resource
    private IGwContractService gwContractService;

    /**
     * 查询客户管理
     *
     * @param id 客户管理主键
     * @return 客户管理
     */
    @Override
    public GwCustomerInfo selectGwCustomerInfoById(String id) {
        GwCustomerInfo gwCustomerInfo = gwCustomerInfoMapper.selectGwCustomerInfoById(id);
        //合同信息
        List<GwContract> gwContracts = gwContractService.getGwContractByTypeAndUserId("合同管理", id, null);
        gwCustomerInfo.setGwContracts(gwContracts);
        return gwCustomerInfo;
    }

    /**
     * 查询客户管理列表
     *
     * @param gwCustomerInfo 客户管理
     * @return 客户管理
     */
    @Override
    public List<GwCustomerInfo> selectGwCustomerInfoList(GwCustomerInfo gwCustomerInfo) {
        //设置公司ID
        System.out.println("-----applet login enterpriseId:"+SecurityUtils.getLoginUser().getEnterpriseId()+"-------------------");
        String companyId = ObjectUtils.isNotEmpty(SecurityUtils.getLoginUser().getEnterpriseId()) ? SecurityUtils.getLoginUser().getEnterpriseId() : "-1";
        gwCustomerInfo.setCompanyId(companyId);
        //企业管理员看公司的
        if(!"1".equals(SecurityUtils.getLoginUser().getUser().getUserType())){
            //其他人看自己创建的
            gwCustomerInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        }
        return gwCustomerInfoMapper.selectGwCustomerInfoList(gwCustomerInfo);
    }

    /**
     * 新增客户管理
     *
     * @param gwCustomerInfo 客户管理
     * @return 结果
     */
    @Override
    public int insertGwCustomerInfo(GwCustomerInfo gwCustomerInfo) {
        // 设置公司ID
        if(ObjectUtils.isEmpty(SecurityUtils.getLoginUser().getEnterpriseId())){
            throw new GlobalException("未知的公司,请绑定公司再进行操作");
        }
        gwCustomerInfo.setCompanyId(SecurityUtils.getLoginUser().getEnterpriseId());
        //gwCustomerInfo.setId(IdUtils.fastSimpleUUID());
        gwCustomerInfo.setCreateTime(DateUtils.getNowDate());
        gwCustomerInfo.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        gwCustomerInfo.setUpdateTime(DateUtils.getNowDate());
        gwCustomerInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        return gwCustomerInfoMapper.insertGwCustomerInfo(gwCustomerInfo);
    }

    /**
     * 修改客户管理
     *
     * @param gwCustomerInfo 客户管理
     * @return 结果
     */
    @Override
    public int updateGwCustomerInfo(GwCustomerInfo gwCustomerInfo) {
        gwCustomerInfo.setUpdateTime(DateUtils.getNowDate());
        gwCustomerInfo.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        return gwCustomerInfoMapper.updateGwCustomerInfo(gwCustomerInfo);
    }

    /**
     * 批量删除客户管理
     *
     * @param ids 需要删除的客户管理主键
     * @return 结果
     */
    @Override
    public int deleteGwCustomerInfoByIds(String[] ids) {
        //校验是否存在合同
        StringBuffer sb = new StringBuffer();
        for (String id : ids) {
            List<GwContract> contract = gwContractService.getGwContractByTypeAndUserId("合同管理", id, null);
            if(ObjectUtils.isNotEmpty(contract)){
                GwCustomerInfo gwCustomerInfo = gwCustomerInfoMapper.selectGwCustomerInfoById(id);
                sb.append(gwCustomerInfo.getCustomerName()).append("存在签署的合同，不能删除\n\t");
            }
        }
        if(sb.length() > 0){
            throw new GlobalException(sb.toString());
        }
        return gwCustomerInfoMapper.deleteGwCustomerInfoByIds(ids);
    }

    /**
     * 删除客户管理信息
     *
     * @param id 客户管理主键
     * @return 结果
     */
    @Override
    public int deleteGwCustomerInfoById(String id) {
        return gwCustomerInfoMapper.deleteGwCustomerInfoById(id);
    }
}
